package com.ymatou.iapi.notify.testcase;

import com.ymatou.iapi.notify.parameter.GetMessageListByTimelineNewBean;
import com.ymatou.iapi.notify.parameter.SendNotifyBean;
import com.ymatou.iapi.notify.parameter.emu.MessageIsNew;
import com.ymatou.iapi.notify.service.GetMessageListByTimelineNewCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.NotifyCallService;
import com.ymttest.database.sqlwapper.ImDefaultWapper;
import com.ymttest.utils.YMTDateUtil;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
*获取通知列表新接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetMessageListByTimelineNew{
	private static GetMessageListByTimelineNewBean getmessagelistbytimelinenewBean;
	private static GetMessageListByTimelineNewCall getmessagelistbytimelinenewCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取通知列表新接口");
	}

	@Before
	public void caseUp() {
		getmessagelistbytimelinenewBean=new GetMessageListByTimelineNewBean();
		getmessagelistbytimelinenewCall=new GetMessageListByTimelineNewCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getmessagelistbytimelinenewCall.getOpurl());
	}
	
	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetMessageListByTimelineNew_01() {
		Logger.start(true,"获取通知列表新接口");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			Integer userId = urrc.getUserid();
			SendNotifyBean sendNotifyBean1 = NotifyCallService.initSendNotify(30156393,userId);
			SendNotifyBean sendNotifyBean2 = NotifyCallService.initSendNotify(30156418,userId);
			NotifyCallService.SendNotify(sendNotifyBean1);
			NotifyCallService.SendNotify(sendNotifyBean2);
			String fromUserId = sendNotifyBean1.getNotifySenderUserId() +","+sendNotifyBean2.getNotifySenderUserId();
			getmessagelistbytimelinenewBean.setFromUserId(fromUserId);
			getmessagelistbytimelinenewBean.setToUserId(userId);
			getmessagelistbytimelinenewBean.setIsNew(MessageIsNew.No);
			getmessagelistbytimelinenewBean.setLongTimeline(YMTDateUtil.getDate().getTime());
			getmessagelistbytimelinenewBean.setMessageCount(10);
			getmessagelistbytimelinenewBean.setIsNotClearUnReadCount(true);
			getmessagelistbytimelinenewCall.setData(getmessagelistbytimelinenewBean);
			getmessagelistbytimelinenewCall.callService();			
			Logger.verifyEquals("Success",getmessagelistbytimelinenewCall.getString("Status"),"验证Status");
			verifyDB(getmessagelistbytimelinenewBean);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetMessageListByTimelineNew_02() {
		Logger.start(true,"获取通知列表新接口，通知号不存在");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			Integer userId = urrc.getUserid();
			getmessagelistbytimelinenewBean.setFromUserId("735267");
			getmessagelistbytimelinenewBean.setToUserId(userId);
			getmessagelistbytimelinenewBean.setIsNew(MessageIsNew.No);
			getmessagelistbytimelinenewBean.setLongTimeline(YMTDateUtil.getDate().getTime());
			getmessagelistbytimelinenewBean.setMessageCount(10);
			getmessagelistbytimelinenewBean.setIsNotClearUnReadCount(true);
			getmessagelistbytimelinenewCall.setData(getmessagelistbytimelinenewBean);
			getmessagelistbytimelinenewCall.callService();
			Logger.verifyEquals(true,getmessagelistbytimelinenewCall.getResults().size()==0,"验证返回结果为空");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetMessageListByTimelineNew_03() {
		Logger.start(true,"获取通知列表新接口，接收号不存在");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			Integer userId = urrc.getUserid();
			getmessagelistbytimelinenewBean.setFromUserId("735267");
			getmessagelistbytimelinenewBean.setToUserId(userId);
			getmessagelistbytimelinenewBean.setIsNew(MessageIsNew.No);
			getmessagelistbytimelinenewBean.setLongTimeline(YMTDateUtil.getDate().getTime());
			getmessagelistbytimelinenewBean.setMessageCount(10);
			getmessagelistbytimelinenewBean.setIsNotClearUnReadCount(true);
			getmessagelistbytimelinenewCall.setData(getmessagelistbytimelinenewBean);
			getmessagelistbytimelinenewCall.callService();
			Logger.verifyEquals(true,getmessagelistbytimelinenewCall.getResults().size()==0,"验证返回结果为空");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	public void verifyDB(GetMessageListByTimelineNewBean getMessageListBean) throws IOException {
		String UserLogoUrl = JUserCallService.GetLogoUrl(getMessageListBean.getToUserId());
		String UserLoginId = JUserCallService.GetUserInfo(getMessageListBean.getToUserId()).getLoginId();
		List<Map> mapList = new ImDefaultWapper().selectImNotifyByToUserId(getMessageListBean.getToUserId());
		for (Map map:mapList
		) {
			String notifyId = map.get("NotifyId").toString();
			String messageId = map.get("MsgFromId")+"_"+map.get("MsgToId")+"_"+notifyId;
			getmessagelistbytimelinenewCall.getResults().stream().filter(x->x.getMessageId().equals(messageId)).forEach(p->{
				Logger.verifyEquals(p.getMsgFromId(),Integer.parseInt(map.get("MsgFromId").toString()),"验证通知号");
				Logger.verifyEquals(p.getUserLoginId(),UserLoginId,"验证用户id");
				Logger.verifyEquals(p.getUserLogoUrl(),UserLogoUrl,"验证用户头像");
				Logger.verifyEquals(p.getMessage(),map.get("Message").toString(),"验证Message");
				AssertService.assertTime(YMTDateUtil.parseDate(p.getPostTime(),YMTDateUtil.YMDHMS),YMTDateUtil.getDate(map.get("PostTime").toString(),YMTDateUtil.YMDHMS),5,"验证创建时间");
				String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(p.getLongPostTime()));
				Date date = YMTDateUtil.getDate(time,YMTDateUtil.YMDHMS);
				AssertService.assertTime(date,YMTDateUtil.getDate(map.get("PostTime").toString(),YMTDateUtil.YMDHMS),5,"验证创建时间长整型");
				Logger.verifyEquals(p.getIsRead().toString(),Integer.parseInt(map.get("IsRead").toString()) == 0?"UnRead":"Read","验证是否未读");
			});
		}
	}
}